package com.light.hessian.provider.discovery;

import com.light.hessian.provider.config.ProviderInfo;
import com.light.hessian.provider.config.ProviderStatus;
import com.light.hessian.provider.slot.*;
import com.light.hessian.remoting.client.balance.Balance;
import com.light.hessian.remoting.client.balance.BalancerHolder;
import com.light.hessian.remoting.client.balance.BaseBalancer;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public  class PreparedStatusSlotPiple implements ProviderStatusSlotPiple {

    protected static final Logger logger= LoggerFactory.getLogger(PreparedStatusSlotPiple.class);

    private ProviderStatusSlotHolder statusSlotHolder;
    private BalancerHolder balancerHolder;
    private Balance balance;

    public PreparedStatusSlotPiple() {
    }

    public PreparedStatusSlotPiple(ProviderStatusSlotHolder statusSlotHolder, BalancerHolder balancerHolder, Balance balance) {
        this.statusSlotHolder = statusSlotHolder;
        this.balancerHolder = balancerHolder;
        this.balance = balance;
    }

    public ProviderStatusSlotHolder getStatusSlotHolder() {
            return statusSlotHolder;
        }

        public void setStatusSlotHolder(ProviderStatusSlotHolder statusSlotHolder) {
            this.statusSlotHolder = statusSlotHolder;
        }

        @Override
        public List<ProviderStatusSlot> pullStatusSlots(String provider) {
            return statusSlotHolder.getProviderStatusSlots();
        }

        @Override
        public void reportSlotStatus(ProviderInfo providerInfo, ProviderStatus status) {
            List<ProviderStatusSlot> statusSlots = statusSlotHolder.getProviderStatusSlots();
            if (CollectionUtils.isNotEmpty(statusSlots)){
                for (ProviderStatusSlot statusSlot:statusSlots) {
                    if (statusSlot.getBaseProviderInfo().equals(providerInfo)){
                        logger.debug("{} is {}",providerInfo,status);
                        statusSlot.setStatus(status);
                    }
                }
            }
        }

        @Override
        public void reportSlotRuntime(ProviderRunStatus runStatus) {
            List<ProviderStatusSlot> statusSlots = statusSlotHolder.getProviderStatusSlots();
            if (CollectionUtils.isNotEmpty(statusSlots)){
                for (ProviderStatusSlot statusSlot:statusSlots) {
                    if (statusSlot.getBaseProviderInfo().equals(runStatus.getProviderInfo())){
                        SlotRuntime runtime = statusSlot.getRuntime().clone();
                        //上报运行时间
                        runtime.setEnterTime(runStatus.getStartTime());
                        runtime.setOutTime(runStatus.getEndTime());
                        statusSlot.setRuntime(runtime);
                    }
                }

            }
        }

        @Override
        public ProviderStatusSlot pullStatusSlot(Balance balance, String provider) {
            if(balance==null){
                balance=getBalance();
            }
            BaseBalancer balancer=balancerHolder.getBalancer(balance.name());
            return  balancer.select();
        }

        public Balance getBalance() {
            return balance;
        }

        public void setBalance(Balance balance) {
            this.balance = balance;
        }

        public BalancerHolder getBalancerHolder() {
            return balancerHolder;
        }

        public void setBalancerHolder(BalancerHolder balancerHolder) {
            this.balancerHolder = balancerHolder;
        }
    }